import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/data/cache/auth_cache.dart';
import 'package:wu_you/data/cache/login_cache.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/page/page_main.dart';
import 'package:wu_you/page/splash/page_login.dart';
import 'package:wu_you/route/app_routers.dart';
import 'package:wu_you/utils/navigator_util.dart';
import 'package:wu_you/utils/utils.dart';

class LoginUtil {

  ///加载全局用户信息
  static void initGlobalInfo(BuildContext context) {
    // UserInfoUtil.obtainUserInfo(context);
    LoginCache().save(true);
  }


  ///跳进登录次数
  static int _onlyOne = 0;
  static Future<dynamic> jumpLogin() async {
    _onlyOne ++;
    if(_onlyOne == 1) {
      _onlyOne = 0;
      MainIndexCubit cubit = BlocProvider.of(AppGlobalKeys.navigatorKey.currentContext);
      if(Navigator.canPop(AppGlobalKeys.navigatorKey.currentContext) && cubit.state != 0){
        Navigator.pop(AppGlobalKeys.navigatorKey.currentContext);
      }
      await LoginCache().save(false);
      // AuthTokenCache().clear();
      // Http().token = null;
      // return AppGlobalKeys.navigatorKey.currentState.push(MaterialPageRoute(fullscreenDialog: true, builder: (context) => LoginPage()));
      return  Navigator.push(
          AppGlobalKeys.navigatorKey.currentContext,
          MaterialPageRoute(fullscreenDialog: true, builder: (context) => LoginPage()));
    }
  }


  ///退出操作
  static void logout({BuildContext context}) {
    AuthTokenCache().clear();
    LoginCache().save(false);
    Http().token = null;
    Http().language = null;
    NavigatorUtil.navigateToPage(context, LoginPage());
  }
}
